
#include <math.h>
#include "thermistorTable.h"

double getCelsius(int rawADC)
{
	/*
	double temp;
	temp = log(((10240000/rawADC) - 10000));
	temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * temp * temp ))*	temp );
	temp -= 273.15;
*/
	float readVoltage 	= (rawADC * ARDUINO_POWER_SUPPLY) / 1024;
	float resistance 	= (readVoltage * THERMISTOR_RESISTOR_VALUE)/ (ARDUINO_POWER_SUPPLY - readVoltage);
	
	float temp = getTemperatureFromLookupTable(resistance);
	
//	double temp = rawADC;
	MINIMUM_TEMPERATURE = min(MINIMUM_TEMPERATURE, temp);
	MAXIMUM_TEMPERATURE = max(MAXIMUM_TEMPERATURE, temp);
	return temp;
	
}
//=========================================================================================================
float getTemperatureFromLookupTable(int resistance)
{
	int i = 0; 
	while (thermistorTable[i].resistance > resistance && i < 39)
		i++;
	return getLinearizedValueBetweenTwoPoints( 
	thermistorTable[i].resistance,thermistorTable[i].temperature,
	thermistorTable[i+1].resistance,thermistorTable[i+1].temperature,
	resistance);
//	return thermistorTable[i].temperature;
}
//=========================================================================================================
float getLinearizedValueBetweenTwoPoints(float x1,float y1,float x2,float y2,float x)
{
	return ((y2-y1)/(x2-x1))*(x-x1)+y1;
}
//=========================================================================================================
